;
(function () {
  'use strict';
  angular.module('services.userBehaviorAnalysis.userChurnAnalysis')
    .service('userLossDIAnalysisService', userLossDIAnalysisService);
  function userLossDIAnalysisService($http, $filter, CoreService, $timeout, CoreLoading) {
    var time = new Date();
    var date = new Date(time.getFullYear(), time.getMonth(), time.getDate() - 61);  //默认当前日期的前2个月
    var that = this;
    //页面参数
    this.params = {
      startTime: date,
      endTime: date,
      provinceCodes: 0,  //省公司code
      businessCodes: 0   //业务线code
    };

    this.send = {
      page: {
        currentPage: 1,
        pageSize: '10',
        totalCount: 40
      }
    };
    this.pageItems = ['10', '20', '40'];

    this.businessLineList = [];   //业务线列表
    this.provincialCompanyList = [];   //省公司数据列表
    this.userLoseDIList = [];  //流失用户具体信息数据集合
    this.showSearchPanel = true;//显示查询面板
    this.arrow = false;
    this.showIcon = true;   //控制显示折线和表格的图标
    this.showDataByHourBtn = false;   //控制是否显示按时刻展示数据的变量
    this.viewType = '';//默认折线视图类型
    this.title = null;  //图表和表格显示的标题

    var dataList = [];   //临时数据集合
    var proName = '全部';//(选中省公司名称)
    var blName = '全部';//(选中业务线名称)

    var param = {}; //临时参数对象
    /**
     * 隐藏查询面板
     */
    this.hideSearchPanel = function () {
      this.showSearchPanel = false;
    };
    /**
     * 清空页面旧值
     */
    this.reset = function () {
      that.arrow = false;
      that.showSearchPanel = true; //控制隐藏显示查询面板
      that.viewType = '';
      that.params.startTime = date;
      that.params.endTime = date;
      that.params.businessCodes = 0;
      that.params.provinceCodes = 0;
      that.userLoseDIList = [];
      that.dataList = [];
      proName = '全部';
      blName = '全部';
      that.showIcon = false;
      that.businessLineList = '';   //业务线列表
      that.provincialCompanyList = [];   //省公司数据列表
    };


    /**
     * 页面初始化 业务线和警告类型下拉框
     */
    this.initSelectMenu = function () {
      //获取业务线
      $http.get('/sas/common/tbm_businessLineQuery')
        .then(function (res) {
          if (res.data.state === '10000' && res.data.data.length >= 1) {
            that.businessLineList = res.data.data;
            //获取省公司
            $http.get('/sas/common/provinceQuery')
              .then(function (res) {
                if (res.data.state === '10000' && res.data.data.length >= 1) {
                  that.provincialCompanyList = res.data.data;
                  that.search();
                } else {
                  CoreService.dialog('查询无数据');
                }
              });
          } else {
            CoreService.dialog('查询无数据');
          }
        });
    };

    /**
     * 查询 通过告警返回码分析判断业务故障
     */
    this.search = function (isPage) {
      //收起查询面板
      if (!that.showSearchPanel && !isPage) {
        that.showSearchPanel = true;
        return;
      }
      //copy页面参数 格式化参数
      param = angular.copy(that.params);
      param.startTime = $filter('date')(param.startTime, 'yyyy-MM-dd');
      param.endTime = $filter('date')(param.endTime, 'yyyy-MM-dd');

      if (param.provinceCodes === 0) {
        proName = '全部';
      } else {
        that.provincialCompanyList.map(function (obj) {
          if (param.provinceCodes === obj.provinceCode) {
            proName = obj.provinceName;
          }

        });
      }

      if (param.businessCodes === 0) {
        blName = '全部';
      } else {
        that.businessLineList.map(function (obj) {

          if (param.businessCodes === obj.businessLineCode) {
            blName = obj.businessLineName;
          }

        });
      }
      //debugger;
      //如果选择‘全部’则传所有省公司  否则就传选择的
      param.provinceCodes = param.provinceCodes != 0 ? [param.provinceCodes] : that.provincialCompanyList.map(function (obj) {
        return obj.provinceCode;
      });
      //如果选择‘全部’则传所有业务线  否则就传选择的
      param.businessCodes = param.businessCodes != 0 ? [param.businessCodes] : that.businessLineList.map(function (obj) {
        return obj.businessLineCode;
      });
      //页面标题显示方式
      if (blName !== '全部' && proName !== '全部') {
        if(param.startTime===param.endTime){
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日 ') + blName + proName + ' 流失用户基本信息';
        }else{
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + '  至  ' + $filter('date')(that.params.endTime, 'yyyy年MM月dd日')
             + blName + proName + ' 流失用户基本信息';
        }
      } else if (blName === '全部' && proName === '全部') {
        if(param.startTime===param.endTime){
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + ' 流失用户基本信息';
        }else{
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + '  至  ' + $filter('date')(that.params.endTime, 'yyyy年MM月dd日')
          + '流失用户基本信息';
        }
      } else if (blName === '全部' && proName !== '全部') {
        if(param.startTime===param.endTime){
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + proName + ' 流失用户基本信息';
        }else{
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + '  至  ' + $filter('date')(that.params.endTime, 'yyyy年MM月dd日')
          + proName + '流失用户基本信息';
        }
      } else {
        if(param.startTime===param.endTime){
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + blName + ' 流失用户基本信息';
        }else{
          that.title = $filter('date')(that.params.startTime, 'yyyy年MM月dd日') + '  至  ' + $filter('date')(that.params.endTime, 'yyyy年MM月dd日')
          + blName + '流失用户基本信息';
        }
      }
      param.pageSize = parseInt(that.send.page.pageSize);
      if(that.showSearchPanel){
        param.currentPage = 1;
      }else{
        param.currentPage = that.send.page.currentPage;
      }
      CoreLoading.setState(true);
      $http.post('/sas/analysisUserBehavior/userChurnRate/queryChurnInfo', param)
        .then(function (res) {
          that.showSearchPanel = false;   //收缩查询面板
          if (res.data.state === '10000' && res.data.data.resultList.length >= 1) {
            dataList = res.data.data.resultList;
            that.send.page.currentPage = res.data.data.currentPage;
            that.send.page.totalCount = res.data.data.totalCount;
            that.send.page.pageSize = res.data.data.pageSize + '';
            that.arrow = false;
            that.changeViewType('table');  //默认显示折线图表
            that.showIcon = true; //显示表格和折线图标
          } else {
            that.PageDisplayStyle();
            CoreService.dialog('查询无数据');
            that.arrow = true;
          }
          CoreLoading.setState(false);
        });
    };

    /**初始化表格*/
    function initTable() {
      var list = [];
      var row = [];
      angular.forEach(dataList, function (data) {
        row = [];
        row.push(data.businessName, data.provinceName, data.phone, data.rechargeFre, data.avgAmounts, data.lastRechargeDate, data.lastRechargeAmount);
        list.push(row);
      });
      that.userLoseDIList = list;
    };

    //查询无数据时页面的样式
    this.PageDisplayStyle = function () {
      that.showSearchPanel = true;   //显示查询面板
      that.showIcon = false;  //隐藏表格和折线图标
      dataList = [];  //数据清空
      that.userLoseDIList = [];
      that.viewType='';
    }
    //控制显示折线或者表格
    this.changeViewType = function (type) {
      that.viewType = type || that.viewType;
      if (that.viewType === 'chart') {
        setTimeout(initChart, 0);
      } else if (that.viewType === 'table') {
        initTable();
      }
    };
    /**导出excel*/
    this.sendData = {date: ''};
    this.onSubmit = function () {
      var data = {
        startTime: $filter('date')(that.params.startTime, 'yyyy-MM-dd'),
        endTime: $filter('date')(that.params.endTime, 'yyyy-MM-dd'),
        title: that.title,
        provinceCodes: param.provinceCodes,
        businessCodes: param.businessCodes/*,
         base64s: [chart.getDataURL('png').split(',')[1]],*/
      };
      that.sendData = JSON.stringify(data);
    };
  }

  userLossDIAnalysisService.$inject = ['$http', '$filter', 'CoreService', '$timeout', 'CoreLoading'];
})
();